#include "ROFramedUpdater.h"
#include "ROSyncProperty.h"
ROFramedUpdater::ROFramedUpdater(QObject *parent):ROPropertyUpdater(parent)
{
    setSafePolicy(ROPropertyUpdater::SafePolicy::Fast);
    timer = new QTimer(this);
    timer->setInterval(1000 / 20);
    connect(timer,&QTimer::timeout,this,&ROFramedUpdater::update);
    auto validEnable = [=](){
        if(enabled())
        {
            timer->start();
        }
        else
        {
            timer->stop();
        }
    };
    connect(this,&ROFramedUpdater::enabledChanged,timer,validEnable);
    validEnable();
}

int ROFramedUpdater::getInterval() const
{
    return timer->interval();
}

void ROFramedUpdater::setInterval(int newInterval)
{
    if (timer->interval() == newInterval)
        return;
    timer->setInterval(newInterval);
    emit intervalChanged();
}

QObject *ROFramedUpdater::registerUpdater(ROSyncProperty *source)
{
    QObject* reg = new QObject();
    connect(this,&ROFramedUpdater::update,reg,[=](){
        updateProperty(source,source->allProperties());
    });

    return reg;
}
